string LCDNameTag = "LCD"; //LCD显示器名字，所有名字和这个完全匹配的LCD会被程序使用。设置为ALL时程序会使用所有LCD
double ResolutionRatio = 0.1; //字体大小（字体可以理解为像素，字体越小像素越小，屏幕像素越多）
bool IsWildPanel = false; //是否是宽屏LCD
Vector3I BackgroundColor = new Vector3I(0,0,0); //背景颜色RGB
Vector3I FontColor = new Vector3I(255,255,255); //字体颜色RGB
int TextSpacing = 3; //字符间距
Vector2I StartPoint = new Vector2I(60,80); //字符起始坐标，方形LCD在0.1字体大小时长宽像素都是177
string[] NumberPixel = {
	"11111 10001 10001 10001 10001 10001 11111", //0
	"00100 00100 00100 00100 00100 00100 00100", //1
	"11111 00001 00001 11111 10000 10000 11111", //2
	"11111 00001 00001 11111 00001 00001 11111", //3
	"10001 10001 10001 11111 00001 00001 00001", //4
	"11111 10000 10000 11111 00001 00001 11111", //5
	"11111 10000 10000 11111 10001 10001 11111", //6 
	"11111 00001 00001 00001 00001 00001 00001", //7
	"11111 10001 10001 11111 10001 10001 11111", //8
	"11111 10001 10001 11111 00001 00001 11111" //9
};
string ColonPixel = "00000 00100 00000 00000 00100 00000 00000"; //冒号的样式

//使用Time=h:m:s指令可以设置时间，例如 Time=23:5:0


int t; //帧计时器，每一帧+1，默认值是0

int s; //秒
int m; //分
int h; //时

bool init;
void Main(string arguments)
{
	if(!init){GetBlocks();return;}
	
	//处理时间的增加
	t ++;
	if(t == 60){
		s += 1;
		t = 0;
	}
	if(s == 60){
		m += 1;
		s = 0;
	}
	if(m == 60){
		h +=1;
		m = 0;
	}
	if(h == 24){
		h = 0;
	}
	
	if(arguments.StartsWith("Time=")){
		try{
			int.TryParse(arguments.Split('=')[1].Split(':')[0], out h);
			int.TryParse(arguments.Split('=')[1].Split(':')[1], out m);
			int.TryParse(arguments.Split('=')[1].Split(':')[2], out s);
		}
		catch{}
	}
	
	string info = h + ":" + m + ":" + s;
	Echo(info);
	
	ShowTimeOnLCD();
}

List<IMyTextPanel> LCDs = new List<IMyTextPanel>();
void GetBlocks()
{
	GridTerminalSystem.GetBlocksOfType(LCDs, b => (LCDNameTag == "" ? false : (LCDNameTag == "ALL" ? true : b.CustomName == LCDNameTag)));
	init = true;
}

void ShowTimeOnLCD()
{
	int PixHeight = (int)Math.Round(17.7/ResolutionRatio,0);
	int PixWidth = (int)Math.Round(17.7/ResolutionRatio,0);
	if(IsWildPanel){
		PixWidth *= 2;
	}
	
	char[][] Pixs = new char[PixHeight][]; //整个像素数组
	//渲染背景
	for(int y = 0; y < PixHeight; y ++)
	{
		Pixs[y] = new string(GetColorFromRGB(BackgroundColor.X,BackgroundColor.Y,BackgroundColor.Z), PixWidth).ToCharArray();
	}
	
	//获取字符串的像素坐标
	int nowWidth = 0;
	List<Vector2I> Text = new List<Vector2I>();
	Text.AddList(DrawStringText(NumberPixel[(int)Math.Floor((double)h/10)], StartPoint.X, StartPoint.Y)); //绘制小时十位
	nowWidth += GetStringTextWidth(NumberPixel[(int)Math.Floor((double)h/10)]) + TextSpacing;
	Text.AddList(DrawStringText(NumberPixel[h%10], StartPoint.X + nowWidth, StartPoint.Y)); //绘制小时个位
	nowWidth += GetStringTextWidth(NumberPixel[h%10]) + TextSpacing;
	
	Text.AddList(DrawStringText(ColonPixel, StartPoint.X + nowWidth, StartPoint.Y)); //绘制冒号
	nowWidth += GetStringTextWidth(ColonPixel) + TextSpacing;
	
	Text.AddList(DrawStringText(NumberPixel[(int)Math.Floor((double)m/10)], StartPoint.X + nowWidth, StartPoint.Y)); //绘制分钟十位
	nowWidth += GetStringTextWidth(NumberPixel[(int)Math.Floor((double)m/10)]) + TextSpacing;
	Text.AddList(DrawStringText(NumberPixel[m%10], StartPoint.X + nowWidth, StartPoint.Y)); //绘制分钟个位
	nowWidth += GetStringTextWidth(NumberPixel[m%10]) + TextSpacing;
	
	Text.AddList(DrawStringText(ColonPixel, StartPoint.X + nowWidth, StartPoint.Y)); //绘制冒号
	nowWidth += GetStringTextWidth(ColonPixel) + TextSpacing;
	
	Text.AddList(DrawStringText(NumberPixel[(int)Math.Floor((double)s/10)], StartPoint.X + nowWidth, StartPoint.Y)); //绘制秒钟十位
	nowWidth += GetStringTextWidth(NumberPixel[(int)Math.Floor((double)s/10)]) + TextSpacing;
	Text.AddList(DrawStringText(NumberPixel[s%10], StartPoint.X + nowWidth, StartPoint.Y)); //绘制秒钟个位
	nowWidth += GetStringTextWidth(NumberPixel[s%10]) + TextSpacing;
	
	
	
	//渲染字符串
	foreach(Vector2I v in Text){
		Pixs[v.Y][v.X] = GetColorFromRGB(FontColor.X, FontColor.Y, FontColor.Z);
	}
	
	//渲染成LCD的内容
	string OutPutString = "";
	for(int y = 0; y < PixHeight; y ++)
	{
		OutPutString += new string(Pixs[y]) + "\n";
	}
	
	//执行显示
	foreach(IMyTextPanel LCD in LCDs){
		LCD.SetValue("Font",1147350002L);
		LCD.SetValue("FontSize", (float)ResolutionRatio);
		LCD.ShowPublicTextOnScreen();
		LCD.WritePublicText(OutPutString);
	}
}

//根据RGB色值获取char
public char GetColorFromRGB(double R, double G, double B)    //Large RGB Convertor(0 - 225)
{
	R /= 32;
	G /= 32;
	B /= 32;
	byte CR = (byte)(R > 255 ? 255 : (R < 0 ? 0 : R));
	byte CG = (byte)(G > 255 ? 255 : (G < 0 ? 0 : G));
	byte CB = (byte)(B > 255 ? 255 : (B < 0 ? 0 : B));
	return (char)(0xE100 + (CR << 6) + (CG << 3) + CB);
}

//固定字符串转像素
public List<Vector2I> DrawStringText(string inputText, int XOffset = 0, int YOffset = 0)
{
	List<Vector2I> res = new List<Vector2I>();
	
	string[] thisNumber = inputText.Split(' ');
	for(int i = 0; i < thisNumber.Length; i ++){
		for(int j = 0; j < thisNumber[i].Length; j ++){
			if(thisNumber[i].Substring(j,1) == "1"){
				res.Add(new Vector2I(j+XOffset,i+YOffset));
			}
		}
	}
	return res;
}

//获取固定字符串的宽度
public int GetStringTextWidth(string inputText)
{
	string[] thisNumber = inputText.Split(' ');
	if(thisNumber.Length <= 0){
		return 0;
	}
	return thisNumber[0].Length;
}

//字符串转像素
public List<Vector2I> DrawTextLine(string Input_Text, int X1, int Y1)  //这是一个很不耐烦的函数，碰到\n就会退出
{
	int XShift = X1;
	int YShift = Y1;

	List<Vector2I> L_DotList = new List<Vector2I>();

	for (int iText = 0; (iText < Input_Text.Length); iText++)
	{
		char CharStep = Input_Text[iText];
		switch (CharStep)
		{
		case ' ':
			XShift += 4;
			break;
		case '\n':
			XShift = 0;
			YShift += 6;
			continue;   //遇到\n直接退出
		default:
			short CharBitmap = T_GetBitmap(CharStep);
			int i = 14;
			do
			{
				if ((CharBitmap & 1) == 1)
				{
					L_DotList.Add(new Vector2I(XShift + i % 3, YShift - 4 + i / 3));
				}
				CharBitmap >>= 1;
				i--;
			}
			while (CharBitmap > 0);
			XShift += 4;
			//Thanks to Blargmode, he taught me how to use it
			break;
		}
	}
	return L_DotList;
}
public const short iCharOffset = 0x21;
public short T_GetBitmap(char Input_Char)  //非静态
{
	if (Input_Char > 127)
	{
		return CError[1];   //缺少错误提示&错误处理
	}
	return CChar[Input_Char - iCharOffset];
}
public short[] CError =    //k社使我被迫删了const
{
	4095,	//0全色填充
	2047,	//1非ASCII识别错误（1号NoBitmap错误）
	3071,	//2
	3583,	//3
	3839,	//4
	3967,	//5
	4031,	//6
	4063,	//7
	4079,	//8
	4087,	//9
	4091,	//10
	4093,	//11
	4094	//12
};
public short[] CChar =     //k社使我被迫删了const
{9346,23040,24445,15602,17057,10923,9216,5265,17556,21824,1488,20,448,2,672,31599,11415,25255,29326,23497,31118,10666,29330,10922,10954,1040,1044,5393,3640,17492,25218,15203,11245,27566,14627,27502,31143,31140,14827,23533,29847,12906,23469,18727,24557,27501,11114,27556,11131,27565,14478,29842,23403,23378,23549,23213,23186,29351,13459,2184,25750,10752,7,17408,239,18862,227,4843,1395,14756,1886,18861,8595,4302,18805,25745,509,429,170,1396,1369,228,1934,18851,363,362,383,341,2766,3671,5521,9234,17620,1920};


//设置程序自动开始运行
public Program(){
	Runtime.UpdateFrequency = UpdateFrequency.Update1;//激活自触发 
}